<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/** 
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PurchaseController extends AdminController {

    /**
     * 采购 
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
  
    public function index(){
       	  
        $this->display();
    }

    public function purchase(){
         $map['is_delete'] = 0;   
         $map['status'] = 1;   
	     $info = M('supplier')->where($map)->select();  
         $li = M("product")->where($map['is_delete'])->select();
         $lists = M('material')->where($map)->select(); 
	     $count = M("Purchase")->order('id')->count();
    	 import('ORG.Util.Page');
    	 $per_page = 10;
    	 $pg = new \Think\Page($count,$per_page);
    	 $page_show = $pg->show(); 
    	 $limit = $pg->firstRow.",".$pg->listRows; 
    	 $list = M("purchase as a")
         ->field('b.name as material_name,a.id,b.is_magnetic,a.buId,a.material_id,a.billNo,a.uid,a.userName,a.status,a.product_no,a.product_type,a.thickness,a.width,a.price,a.totalAmount,a.billDate,a.description,a.productNo,a.weight,c.name,d.name as subpplier_name')
         ->join('left join one_material as b on  a.material_id = b.id')
         ->join('left join one_product as c on  a.product_no = c.number')
         ->join('left join one_supplier as d on  a.buId = d.id')
         ->order('status DESC')
         ->limit($limit)
         ->select();   
         $this->assign("list",$list);
         $this->assign("info",$info);
         $this->assign("lists",$lists);
    	 $this->assign("li",$li);
    	 $this->assign("page",$page_show);	      	 	     	 	     	 
	     $this->display();
            
    }
    public function delpurchase(){
    	
    	$id = $_POST['id'];
    	$info = M("Purchase")->find($id);
    	$info["isDelete"] = 1;
    	M("Purchase")->save($info);	
    }
    public function add(){
    	   
             $map["material_id"]  = $_POST['material_id'];
             $map["product_no"] = $_POST['product_no'];
             $map["buId"]         = $_POST['buId']; 
	    	 $map["product_type"] = $_POST['product_type'];
	    	 $map["thickness"]    = $_POST['thickness'];
	    	 $map["width"]        = $_POST['width'];
	    	 $map["weight"]       = $_POST['weight'];
	    	 $map["price"]        = $_POST['price'];
	        if(IS_POST){
                // print_r($_POST);die;
	        	$map['billDate'] = time();
	        	$map['productNo'] = "CG".date("Ymd").mt_rand();
	        	M("Purchase")->add($map);
                // print_r(M()->getlastsql());die;

	        }	    	 
	    	 $info = M("Purchase")->order('id')->select();
	     	 $this->assign('info',$info);
	    	 
    }

    public function GetRandStr($len)   
    {   
        $chars = array(   
             "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"   
        );   
        $charsLen = count($chars) - 1;   
        shuffle($chars);     
        $output = "";   
        for ($i=0; $i<$len; $i++)   
        {   
            $output .= $chars[mt_rand(0, $charsLen)];   
        }    
        return $output;    
    }      
    public function delfinance(){ 
           $arr=$this->GetRandStr(4); 
         $data['purchase_id'] = $_GET['id']; 
         $data['productNo'] =   'SC'.date('YmdHis' ,time()).$arr;  //订单编号
         $data['goods_id'] = $data['goods_id']; 
         $data['estimate_amount'] = $data['estimate_amount']; 
         $data['total_amount'] = $data['total_amount']; 
         $data['draw_amount'] = $data['draw_amount']; 
         $data['inferior_amount'] = $data['inferior_amount']; 
         $data['product_time'] = time(); 
         $data['create_time'] = time();  
         $res = M("product")->add($data); 
        if($res == true){
            $this->success('投入成功');
        }
        else{
            $this->error('投入失败');
        }   
    }
 

 











}
